﻿using CC.Extending;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CcMath
{
    public struct StartStop
    {
        public float start;
        public float stop;
        public StartStop(float start,float stop)
        {
            this.start = start;
            this.stop = stop;
        }
    }
    /// <summary>
    /// 起-始-数量数据模型
    /// </summary>
    public class StartStopNum
    {
        public float Start { get; set; }
        public float Stop { get; set; }
        public int Num { get; set; }
        public StartStopNum()
        {
            Start = 0;
            Stop = 0;
            Num = 0;
        }
        public StartStopNum(double start, double stop, int num)
        {
            Start = (float)start;
            Stop = (float)stop;
            Num = num;
        }
        /// <summary>
        /// 返回start:stop@points 格式字符串
        /// </summary>
        /// <param name="formatString">接受"start:stop#num"(默认)或"start:stop:step"格式化字符串</param>
        /// <returns></returns>
        public string ToString(string formatString = "start:stop#num")
        {
            if (Regex.IsMatch(formatString, ".*:.*#.*"))
            {
                if (Num > 1)
                {
                    return $"{Start}:{Stop}#{Num}";
                }
                else
                {
                    return $"{Start}:{Stop}";
                }
            }
            else if (Regex.IsMatch(formatString, ".*:.*:.*"))
            {
                if (Num > 1)
                {
                    float step = (Stop - Start) / (Num - 1);
                    return $"{Start}:{Stop}:{step}";
                }
                else
                {
                    return $"{Start}:{Stop}";
                }
            }
            else
            {
                throw (new Exception("StartStopNum.ToString(): 输入的格式字串：" + formatString + "不被支持"));
            }
        }
        /// <summary>
        /// 将输入的文本解释为start - stop - points格式 
        /// 支持的格式：start:stop:step or start:stop@points
        ///             start:stop  (points=2)
        ///             p1,p2,...pn(取第一个和最后一个，中间点会重新计算）
        /// </summary>
        /// <param name="text"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool TryParse(string text, out StartStopNum value)
        {
            value = new StartStopNum();
            if (Regex.IsMatch(text, ".*[:：].*#.*"))
            {//start:stop#points格式
                string[] segms = text.Split(new char[] { ':', '：', '#' }, StringSplitOptions.RemoveEmptyEntries);
                value.Start = segms[0].ToSingle();
                value.Stop = segms[1].ToSingle();
                value.Num = segms[2].ToInt();
                return true;
            }
            else if (Regex.IsMatch(text, ".*[:：-].*"))
            { //start:stop[:step]或start-stop格式
                string[] segms = text.Split(new char[] { ':', '：', '-' }, StringSplitOptions.RemoveEmptyEntries);
                value.Start = segms[0].ToSingle();
                value.Stop = segms[1].ToSingle();
                if (segms.Length >= 3)
                {
                    float step = segms[2].ToSingle();
                    value.Num = (int)((value.Stop - value.Start) / step) + 1;
                }
                return true;

            }
            else if (Regex.IsMatch(text, ".*[,，].*[,，]*"))
            {// f1,f2,...fn格式(取第一个和最后一个，中间点会重新计算）
                string[] segms = text.Split(new char[] { ',', '，' }, StringSplitOptions.RemoveEmptyEntries);
                value.Start = segms[0].ToSingle();
                value.Stop = segms.Last().ToSingle();
                value.Num = segms.Length;
                return true;
            }
            else
            {
                return false;
            }


        }
    }
}
